#!/bin/sh
#
# description: Starts and stops the RSS Ex 2.0.5
#
mos=/usr/local/etc/mos/rssex205
momenu=/usr/local/etc/moMenu.rss

case "$1" in
  start)
	echo "Starting RSS Ex 2.0.5..."
	cp -Rfp $mos/www/* /tmp/www/
	[ ! -e /usr/local/etc/translate ] && ln -s $mos /usr/local/etc/translate
	;;
  stop)
	echo "Stopping RSS Ex 2.0.5..."
	rm -f /usr/local/etc/translate
	;;

  status)
	if [ -e /usr/local/etc/translate ] ; then
	  echo "RSS Ex 2.0.5 is running"
	else
	  echo "RSS Ex 2.0.5 is stopped"
	fi
	;;

  enable)
	echo "Enabling RSS Ex 2.0.5..."

	# add lib to system
	if [ -z $( cat /etc/ld.so.conf | grep rssex205/lib ) ] ; then
	  echo "$mos/lib" >> /usr/local/etc/ld.so.conf
	  $mos/ldconfig -C /usr/local/etc/ld.so.cache 2>/dev/null
	fi

	# copying files
	#cp -fp  $mos/xspf.conf /usr/local/etc/
	#cp -fp  $mos/xspf.scan /usr/local/etc/
	#cp -fp  $mos/index.m3u /usr/local/etc/

	if [ ! -d /usr/local/etc/playlist ] ; then
	  mkdir /usr/local/etc/playlist
	  #cp -fp $mos/etc/playlists/* /usr/local/etc/playlist/
	fi

	# adding to RSS
	if [ ! -f $momenu ] ; then
	  cp /usr/local/etc/mos/core/rss/moMenu.rss /usr/local/etc/
	fi

	if [ -z "$( cat $momenu | grep etc/rssex205 )" ] ; then
	  sed -i '
/<title>moServices<\/title>/i\
	  <title>RSS Ex 2.0.5</title>\
	  <link>rss_file://../etc/translate/rss/menuEx.rss</link>\
	  <media:thumbnail url="/usr/local/etc/translate/rss/image/menuEx.png" width="120" height="90" />\
	  <mediaDisplay name=photoView />\
	</item>\
	<item>
' $momenu
	fi
	;;

  disable)
	echo "Disabling RSS Ex 2.0.5..."

	# Remove from RSS
	sed -i '
/<title>RSS Ex 2.0.5<\/title>/{N;N;N;N;N;d}
' $momenu

	# Remove files
	[ -d /usr/local/etc/translate ] && rm -f /usr/local/etc/translate

	# remove lib from system
	if [ ! -z $( cat /etc/ld.so.conf | grep rssex205/lib ) ] ; then
	  sed -i '/rssex205/d' /usr/local/etc/ld.so.conf
	  $mos/ldconfig -C /usr/local/etc/ld.so.cache 2>/dev/null
	fi
	;;

  *)
	echo "Usage: $0 {start|stop|status|enable|disable}"
esac

exit $?
